<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
include('../models/entries.php');
include('../models/categories.php');
/**
 * Description of htmlGenerator
 *
 * @author spilavadi
 */
class htmlGenerator {
    //put your code here
    public $template;
    public $colors;
    public $min;
    function  __construct($template) {
        $this->template = $template;
        $this->colors = array('red','green','orange','blue','yellow','maroon','paleYellow');
        $this->min = '';
    }


    function createItem($item)
    {
        $html = file_get_contents('../templates/item.html');
        foreach($item as $tag=>$value)
        {
            $html = str_replace('{{'.$tag.'}}',$value,$html);
        }

        $html = str_replace('{{min}}',$this->min,$html);
        return $html;
    }

    function createBox($title,$items)
    {
        $html = file_get_contents('../templates/box.html');
        $itemsHtml = '';
        foreach($items as $item)
        {
            $item['imageDisplay'] = ($item['images']=='')?'none':'display';
            $itemsHtml .= $this->createItem($item);
        }
        $html = str_replace('{{items}}',$itemsHtml,$html);
        $html = str_replace('{{title}}',  strtoupper($title),$html);
        $html = str_replace('{{color}}',$this->colors[rand(0,count($this->colors)-1)],$html);

        return $html;
    }

    function create()
    {
        $html = file_get_contents('../templates/'.$this->template);
        preg_match_all("([0-9]+)", $html, $matches);
        //echo '<pre>';
        foreach($matches[0] as $catId)
        {
            //echo $catId;
            $cat = getCategory($catId);
            $items = getEntriesByCategory($catId);
            $html = str_replace('{{'.$catId.'}}',$this->createBox($cat['name'],$items),$html);
        }
        $fp = fopen('../'.$this->template, 'w');
        fwrite($fp, $html);
    }

    function createCategory()
    {
        $html = file_get_contents('../templates/'.$this->template);
        $categories = getCategories();
        //echo '<pre>';
        $this->min = 'pmim';
        foreach($categories as $category)
        {
            $items = getEntriesByCategory($category['id'],25);
            //print_r($items);
            if(count($items)>0)
            {
                $html1 = str_replace('{{items}}',$this->createBox($category['name'],$items),$html);
                //$html = str_replace('{{category}}',$category['name'],$html);
                $fp = fopen('../categories/'.$category['name'].'.html', 'w+');
                fwrite($fp, $html1);
                fclose($fp);
            }
        }
    }
}
?>
